package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ProxyInfo;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.hzdesk.media.koreabatchmatvector.adapter.WiredNetworkSettingsAdapter;
import com.hzdesk.media.koreabatchmatvector.bean.WiredNetworkSettingsBean;
import com.hzdesk.media.koreabatchmatvector.logic.contract.IWiredNetworkSettingsContract;
import com.hzdesk.media.koreabatchmatvector.utils.IpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/3.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class WiredNetworkSettingsPersenter implements IWiredNetworkSettingsContract.IWiredNetworkSettingsPersenter {
    private IWiredNetworkSettingsContract.IWiredNetworkSettingsView mIWiredNetworkSettingsView;
    private List<WiredNetworkSettingsBean> mlist;
    private WiredNetworkSettingsAdapter mWiredNetworkSettingsAdapter;

    public WiredNetworkSettingsPersenter(IWiredNetworkSettingsContract.IWiredNetworkSettingsView mIWiredNetworkSettingsView) {
        this.mIWiredNetworkSettingsView = mIWiredNetworkSettingsView;
    }

    @Override
    public void onClickedBack() {
        mIWiredNetworkSettingsView.finshPager();
    }

    @Override
    public void onClickedSave() {
        mIWiredNetworkSettingsView.SaveConfigSettingDate();
    }

    @Override
    public void init(RecyclerView activityWireNetWorkSettingRecylerview, Context mContext) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        activityWireNetWorkSettingRecylerview.setLayoutManager(layoutManager);
        mWiredNetworkSettingsAdapter = new WiredNetworkSettingsAdapter();
        activityWireNetWorkSettingRecylerview.setAdapter(mWiredNetworkSettingsAdapter);
        addEthernetInfo();
    }

    @Override
    public void setCheckBoxListening(AppCompatCheckBox box, Context mContext) {
        if(box.isChecked()){  // 选中
            mIWiredNetworkSettingsView.hidenSettingPager();
        }else{
            mIWiredNetworkSettingsView.showSettingPager();
        }
        box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked){  //选中
                    mIWiredNetworkSettingsView.hidenSettingPager();
                }else{  //取消选中
                    mIWiredNetworkSettingsView.showSettingPager();
                }
            }
        });
    }

    @Override
    public void setEditTextListening(final EditText mEditText, Context mContext) {
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mEditText.setSelection(charSequence.toString().length());
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mEditText.setSelection(charSequence.toString().length());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @SuppressLint("LongLogTag")
    @Override
    public void addEthernetInfo() {
        if(mWiredNetworkSettingsAdapter != null){
            // todo 实际的值要去获取再设置进去
            mlist = new ArrayList<>();
            mlist.add(new WiredNetworkSettingsBean("以太网连接","已连接"));
            mlist.add(new WiredNetworkSettingsBean("IP", IpUtils.getLocalIp()));
            mlist.add(new WiredNetworkSettingsBean("MAC 地址",IpUtils.getMacAddress()));
            mWiredNetworkSettingsAdapter.addItem(mlist);
        }
    }

    @Override
    public void cancel() {
        if(mWiredNetworkSettingsAdapter != null){
            mlist = new ArrayList<>();
            mlist.add(new WiredNetworkSettingsBean("以太网连接","未连接"));
            mlist.add(new WiredNetworkSettingsBean("IP","0.0.0.0"));
            mlist.add(new WiredNetworkSettingsBean("MAC 地址",""));
            mWiredNetworkSettingsAdapter.addItem(mlist);
        }
    }



}
